package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotXfclDeviceBind;
import org.dromara.iot.domain.bo.IotXfclDeviceBindBo;
import org.dromara.iot.domain.vo.IotXfclDeviceBindVo;
import org.dromara.iot.mapper.IotXfclDeviceBindMapper;
import org.dromara.iot.service.IIotXfclDeviceBindService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 车辆与设备绑定关系Service业务层处理
 *
 * @author Luo
 * @date 2025-07-25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class IotXfclDeviceBindServiceImpl implements IIotXfclDeviceBindService {

    private final IotXfclDeviceBindMapper baseMapper;

    /**
     * 查询车辆与设备绑定关系
     *
     * @param id 主键
     * @return 车辆与设备绑定关系
     */
    @Override
    public IotXfclDeviceBindVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询车辆与设备绑定关系列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 车辆与设备绑定关系分页列表
     */
    @Override
    public TableDataInfo<IotXfclDeviceBindVo> queryPageList(IotXfclDeviceBindBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotXfclDeviceBind> lqw = buildQueryWrapper(bo);
        Page<IotXfclDeviceBindVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<IotXfclDeviceBindVo> queryXfclWithDevicePage(IotXfclDeviceBindBo bo, PageQuery pageQuery) {
        Page<IotXfclDeviceBindVo> page = pageQuery.build();
        IPage<IotXfclDeviceBindVo> result = baseMapper.selectXfclWithDevicePage(page, bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的车辆与设备绑定关系列表
     *
     * @param bo 查询条件
     * @return 车辆与设备绑定关系列表
     */
    @Override
    public List<IotXfclDeviceBindVo> queryList(IotXfclDeviceBindBo bo) {
        LambdaQueryWrapper<IotXfclDeviceBind> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotXfclDeviceBind> buildQueryWrapper(IotXfclDeviceBindBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotXfclDeviceBind> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(IotXfclDeviceBind::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getXfclId()), IotXfclDeviceBind::getXfclId, bo.getXfclId());
        lqw.eq(bo.getDeviceId() != null, IotXfclDeviceBind::getDeviceId, bo.getDeviceId());
        return lqw;
    }

    /**
     * 新增车辆与设备绑定关系
     *
     * @param bo 车辆与设备绑定关系
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IotXfclDeviceBindBo bo) {
        IotXfclDeviceBind add = MapstructUtils.convert(bo, IotXfclDeviceBind.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车辆与设备绑定关系
     *
     * @param bo 车辆与设备绑定关系
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IotXfclDeviceBindBo bo) {
        IotXfclDeviceBind update = MapstructUtils.convert(bo, IotXfclDeviceBind.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotXfclDeviceBind entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除车辆与设备绑定关系信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
